package com.my.attendance.controller;

import com.my.attendance.entity.Attendance;
import com.my.attendance.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/sign")
public class SignServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        try {
            String employee_number = req.getParameter("employee_number");
            UserService userService = new UserService();
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date day = simpleDateFormat.parse(simpleDateFormat.format(date));
            if (userService.checkSign(employee_number,day)){
                SimpleDateFormat format1 = new SimpleDateFormat("HH:mm:ss");
                Date start_time = format1.parse(format1.format(date));
                int hours = date.getHours();
                String time_type=hours<=12?"上午":"下午";
                String start_type=hours<=9?"正常":"迟到";
                Attendance attendance = new Attendance(Integer.parseInt(employee_number),day,time_type,start_time,start_type);
                userService.SignUser(attendance);
            }
                req.setAttribute("msg","不能重复签到！");
                req.setAttribute("outmsg","已签到！！！");
                req.getRequestDispatcher("/jsp/welcome.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
